package com.sfac.springCloudAccount.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.account.User;
import com.sfac.account.vo.UserVo;
import com.sfac.common.vo.Result;
import com.sfac.common.vo.Search;
import com.sfac.springCloudAccount.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * UserController
 */
@RestController
@RequestMapping("/api/account")
public class UserController {
	@Autowired
	private UserService userService;

	/**
	 * 127.0.0.1/api/login ---- post
	 * {"userName":"admin","password":"111111"}
	 */
	@PostMapping(value = "/login", consumes = "application/json")
	public Result<User> login(@RequestBody User user) {
		return userService.login(user);
	}

	/**
	 * 127.0.0.1/api/user ---- post
	 */
	@PostMapping(value = "/user", consumes = "application/json")
	public Result<User> insertUser(@RequestBody User user) {
		return userService.insertUser(user);
	}

	/**
	 * 127.0.0.1/api/user ---- put
	 */
	@PutMapping(value = "/user", consumes = "application/json")
	public Result<User> updateUser(@RequestBody User user) {
		return userService.updateUser(user);
	}

	/**
	 * 127.0.0.1/api/user/10 ---- delete
	 */
	@DeleteMapping(value = "/user/{id}")
	public Result<Object> deleteUserById(@PathVariable int id) {
		return userService.deleteUserById(id);
	}

	/**
	 * 127.0.0.1/api/user/10 ---- get
	 */
	@GetMapping(value = "/user/{id}")
	public User getUserById(@PathVariable int id) {
		return userService.getUserById(id);
	}

	/**
	 * 127.0.0.1/api/users ---- post
	 */
	@PostMapping(value = "/users", consumes = "application/json")
	public PageInfo<User> getUsersBySearch(@RequestBody Search search) {
		return userService.getUsersBySearch(search);
	}

	/**
	 * 127.0.0.1:8003/api/userVo/1/1890 ---- get
	 */
	@GetMapping(value = "/userVo/{userId}/{cityId}")
	public UserVo getUserVoByUserIdAndCityId(@PathVariable int userId, @PathVariable int cityId) {
		return userService.getUserVoByUserIdAndCityId(userId, cityId);
	}
}
